package sys.logic;

import sys.transport.*;
import java.util.concurrent.*;
import sys.message.*;

import java.util.*;

public class MessageChecker
{
    Timer t;
    MessageReceiver mr;
    MessageCheckerClient mcc;
    long interval;

    public MessageChecker(MessageReceiver mr, MessageCheckerClient mcc, long interval)
    {
        t = new Timer();
        this.mr = mr;
        this.mcc = mcc;
        this.interval = interval;
    }

    public void start()
    {
        t.schedule(new Check(), 0, interval);
    }

    public void stop()
    {
        t.cancel();
    }

    class Check extends TimerTask
    {
        public void run()
        {
            System.out.println("In message checker timer");
            ConcurrentLinkedQueue <UniMessage> messages = mr.receive();
            if (messages.size() > 0)
                mcc.newMessages(messages);
        }
    }
}
